import numpy as np
import torch
import matplotlib.pyplot as plt

# Prepare dataset
xy = np.loadtxt('../dataset/diabetes.csv', delimiter=',', dtype=np.float32)
x_data = torch.from_numpy(xy[:, :-1])
y_data = torch.from_numpy(xy[:, [-1]])
# print(type(x_data))
# x_data = torch.as_tensor(x_data)
# print(type(x_data))
# y_data = torch.as_tensor(y_data)

# plt.scatter(x_data[:, 1], y_data)
# plt.show()
# Design model using Class
class DiabeteModel(torch.nn.Module):
    def __init__(self):
        super(DiabeteModel, self).__init__()
        # Linear transformation
        self.linear_01 = torch.nn.Linear(8, 6)
        self.linear_02 = torch.nn.Linear(6, 4)
        self.linear_03 = torch.nn.Linear(4, 1)
        # Activate function
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self, x):
        x = self.sigmoid(self.linear_01(x))
        x = self.sigmoid(self.linear_02(x))
        x = self.sigmoid(self.linear_03(x))
        return x


diabeteModel = DiabeteModel()
# Construct loss and optimizer
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(diabeteModel.parameters(), lr=0.01)
# Training cycle
for epoch in range(1000):
    # Forward
    y_pred = diabeteModel(x_data)
    loss = criterion(y_pred, y_data)
    print(epoch, loss.item())
    # Backward
    optimizer.zero_grad()
    loss.backward()
    # Update
    optimizer.step()

print('w1 = ', diabeteModel.linear_01.weight)
print('w2 = ', diabeteModel.linear_02.weight)
print('w3 = ', diabeteModel.linear_03.weight)
print('b1 = ', diabeteModel.linear_01.bias)
print('b2 = ', diabeteModel.linear_02.bias)
print('b3 = ', diabeteModel.linear_03.bias)
